package exttable.formgenerator;

import dbutils.DbUtils;
import exttable.dbclasses.*;
import exttable.testselector.VolunteerSelectorView;
import utils.Localisation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.*;
import java.util.List;


/**
 * Created by IntelliJ IDEA.
 * User: ADMIN
 * Date: 27.05.11
 * Time: 12:14
 * To change this template use File | Settings | File Templates.
 */
public class FormAddProbationer {
    JFrame frame;
    JPanel panel;
    SpringLayout mainLayout;
    JLabel labelVolunteer;
    JComboBox jcbVolunteer;
    JLabel labelGroup;
    JList jListGroup;
    JScrollPane jScrollPane;
    JLabel labelDegreeOfInflammatory;
    JComboBox jcbDegreeOfInflammatory;
    JLabel labelProcessByLocalization;
    JComboBox jcbProcessByLocalization;

    JLabel labelDiseaseDuration;
    JPanel panelDiseaseDuration;
    JRadioButton rbDiseaseDurationYes;
    JRadioButton rbDiseaseDurationNo;
    ButtonGroup bgDiseaseDuration;

    JLabel labelAlcoholic;
    JPanel panelAlcoholic;
    JRadioButton rbAlcoholicYes;
    JRadioButton rbAlcoholicNo;
    ButtonGroup bgAlcoholic;

    JLabel labelNarcomaniac;
    JPanel panelNarcomaniac;
    JRadioButton rbNarcomaniacYes;
    JRadioButton rbNarcomaniacNo;
    ButtonGroup bgNarcomaniac;
    JLabel labelNextButton;
    JButton nextButton;

    public FormAddProbationer() {
        frame = new JFrame();
        panel = new JPanel();
        labelVolunteer = new JLabel(Localisation.getMessage("menu.volunteer"));
        jcbVolunteer = new JComboBox(StaticMethods.getVolunteerList().toArray());
        labelGroup = new JLabel(Localisation.getMessage("table.column.groupcode"));
        try {
            jListGroup = new JList(StaticMethods.getGroupList().toArray());
        } catch (Exception e) {
            e.printStackTrace();
        }
        /*
        labelDegreeOfInflammatory = new JLabel(Localisation.getMessage("labelDegreeOfInflammatory"));
        List<String> keyDegreeOfInflammatory = new ArrayList<String>();
        keyDegreeOfInflammatory.add("test10_instr_chest_x_radiography");
        keyDegreeOfInflammatory.add("degree_of_inflammatory_id");
        keyDegreeOfInflammatory.add("dic_degree_of_inflammatory");
        jcbDegreeOfInflammatory = new JComboBox(VolunteerSelectorView.getMapDicGeneral().get(keyDegreeOfInflammatory).toArray());

        labelProcessByLocalization = new JLabel(Localisation.getMessage("labelProcessByLocalization"));
        List<String> keyListProcessByLocalization = new ArrayList<String>();
        keyListProcessByLocalization.add("test10_instr_chest_x_radiography");
        keyListProcessByLocalization.add("process_by_localization_id");
        keyListProcessByLocalization.add("dic_process_by_localizations");
        jcbProcessByLocalization = new JComboBox(VolunteerSelectorView.getMapDicGeneral().get(keyListProcessByLocalization).toArray());

        labelDiseaseDuration = new JLabel(Localisation.getMessage("labelDiseaseDuration"));
        rbDiseaseDurationYes = new JRadioButton(Localisation.getMessage("form.yes"));
        rbDiseaseDurationNo = new JRadioButton(Localisation.getMessage("form.no"));
        rbDiseaseDurationNo.setSelected(true);
        bgDiseaseDuration = new ButtonGroup();
        bgDiseaseDuration.add(rbDiseaseDurationYes);
        bgDiseaseDuration.add(rbDiseaseDurationNo);
        panelDiseaseDuration = new JPanel();
//        Border border = BorderFactory.createLineBorder(Color.BLACK, 2);
//        panelDiseaseDuration.setBorder(border);
        panelDiseaseDuration.add(rbDiseaseDurationYes);
        panelDiseaseDuration.add(rbDiseaseDurationNo);
        /*
        labelAlcoholic=new JLabel(Localisation.getMessage("labelAlcoholic"));
        rbAlcoholicYes=new JRadioButton(Localisation.getMessage("form.yes"));
        rbAlcoholicNo=new JRadioButton(Localisation.getMessage("form.no"));
        rbAlcoholicNo.setSelected(true);
        bgAlcoholic=new ButtonGroup();
        bgAlcoholic.add(rbAlcoholicYes);
        bgAlcoholic.add(rbAlcoholicNo);
        panelAlcoholic=new JPanel();
        panelAlcoholic.add(rbAlcoholicYes);
        panelAlcoholic.add(rbAlcoholicNo);

        labelNarcomaniac=new JLabel(Localisation.getMessage("labelNarcomaniac"));
        rbNarcomaniacYes=new JRadioButton(Localisation.getMessage("form.yes"));
        rbNarcomaniacNo=new JRadioButton(Localisation.getMessage("form.no"));
        rbNarcomaniacNo.setSelected(true);
        bgNarcomaniac=new ButtonGroup();
        bgNarcomaniac.add(rbNarcomaniacYes);
        bgNarcomaniac.add(rbNarcomaniacNo);
        panelNarcomaniac=new JPanel();
        panelNarcomaniac.add(rbNarcomaniacYes);
        panelNarcomaniac.add(rbNarcomaniacNo);
        */
        panel.add(labelVolunteer);
        panel.add(jcbVolunteer);
        panel.add(labelGroup);
        jScrollPane = new JScrollPane(jListGroup);
        panel.add(jScrollPane);
//        panel.add(labelDegreeOfInflammatory);
//        panel.add(jcbDegreeOfInflammatory);
//        panel.add(labelProcessByLocalization);
//        panel.add(jcbProcessByLocalization);
//        panel.add(labelDiseaseDuration);
//        panel.add(panelDiseaseDuration);
//        panel.add(labelAlcoholic);
//        panel.add(panelAlcoholic);
//        panel.add(labelNarcomaniac);
//        panel.add(panelNarcomaniac);
        labelNextButton = new JLabel();
        panel.add(labelNextButton);
        nextButton = new JButton(Localisation.getMessage("form.continue"));
        nextButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (jListGroup.getSelectedValuesList().size() < 1) {
                    MessageFactory.emptyFields();
                    jListGroup.setBackground(Color.RED);
                    return;
                }
//                ProbationersGroupsV1 pg=new ProbationersGroupsV1();
                ProbationersGroupsV2 pg = new ProbationersGroupsV2();
                pg.setVolunteerId(((Volunteer) jcbVolunteer.getSelectedItem()).getVolunteerId());
                pg.setProbationerCode(jcbVolunteer.getSelectedItem().toString());
                pg.setGroupList(convertArrayToListGroups());
                pg.setSexId(((Volunteer) jcbVolunteer.getSelectedItem()).getSexId());
                try {
                    Connection connGeneral = DbUtils.getDBConnectionGeneral();
                    PreparedStatement psGeneral = connGeneral.prepareStatement("select * from additional_data_for_volunteers where volunteer_id=?");
                    psGeneral.setInt(1, ((Volunteer) jcbVolunteer.getSelectedItem()).getVolunteerId());
                    ResultSet rsGeneral = psGeneral.executeQuery();
                    while (rsGeneral.next()){
                        pg.setDegreeOfInflammatoryId(rsGeneral.getInt("degree_of_inflammatory_id"));
                        pg.setProcessByLocalizationId(rsGeneral.getInt("process_by_localization_id"));
                        pg.setDiseaseDurationMoreThenYear(StaticMethods.convertIntToBoolean(rsGeneral.getInt("disease_duration_more_then_year")));
                    }
                } catch (Exception exp) {
                    exp.printStackTrace();
                }

//                pg.setDegreeOfInflammatoryId(((DicGeneral) jcbDegreeOfInflammatory.getSelectedItem()).getDicId());
//                pg.setProcessByLocalizationId(((DicGeneral) jcbProcessByLocalization.getSelectedItem()).getDicId());
//                pg.setDiseaseDurationMoreThenYear(rbDiseaseDurationYes.isSelected());
//                pg.setAlcoholic(rbAlcoholicYes.isSelected());
//                pg.setNarcomaniac(rbNarcomaniacYes.isSelected());
                pg.setAlcoholic(((Volunteer) jcbVolunteer.getSelectedItem()).isAlcoholic());
                pg.setNarcomaniac(((Volunteer) jcbVolunteer.getSelectedItem()).isNarcomaniac());
                new InsertProbationerForm(pg);
                frame.dispose();
            }
        });
        panel.add(nextButton);
        mainLayout = new SpringLayout();
        panel.setLayout(mainLayout);
        organiseLayout();
        frame.setSize(800, 600);
        frame.add(panel);
        frame.setVisible(true);
        frame.setName("FormAddProbationer");

    }

    private void organiseLayout() {

        mainLayout.putConstraint(SpringLayout.WEST, labelVolunteer, 20, SpringLayout.WEST, panel);
        mainLayout.putConstraint(SpringLayout.NORTH, labelVolunteer, 10, SpringLayout.NORTH, panel);
        mainLayout.putConstraint(SpringLayout.WEST, jcbVolunteer, 250, SpringLayout.EAST, labelVolunteer);
        mainLayout.putConstraint(SpringLayout.NORTH, jcbVolunteer, 10, SpringLayout.NORTH, panel);

        mainLayout.putConstraint(SpringLayout.WEST, labelGroup, 20, SpringLayout.WEST, panel);
        mainLayout.putConstraint(SpringLayout.NORTH, labelGroup, 20, SpringLayout.SOUTH, labelVolunteer);
        mainLayout.putConstraint(SpringLayout.WEST, jScrollPane, 0, SpringLayout.WEST, jcbVolunteer);
        mainLayout.putConstraint(SpringLayout.NORTH, jScrollPane, 20, SpringLayout.SOUTH, jcbVolunteer);

//        mainLayout.putConstraint(SpringLayout.WEST, labelDegreeOfInflammatory, 20, SpringLayout.WEST, panel);
//        mainLayout.putConstraint(SpringLayout.NORTH, labelDegreeOfInflammatory, 20, SpringLayout.SOUTH, jScrollPane);
//        mainLayout.putConstraint(SpringLayout.WEST, jcbDegreeOfInflammatory, 0, SpringLayout.WEST, jScrollPane);
//        mainLayout.putConstraint(SpringLayout.NORTH, jcbDegreeOfInflammatory, 20, SpringLayout.SOUTH, jScrollPane);
//
//        mainLayout.putConstraint(SpringLayout.WEST, labelProcessByLocalization, 20, SpringLayout.WEST, panel);
//        mainLayout.putConstraint(SpringLayout.NORTH, labelProcessByLocalization, 20, SpringLayout.SOUTH, labelDegreeOfInflammatory);
//        mainLayout.putConstraint(SpringLayout.WEST, jcbProcessByLocalization, 0, SpringLayout.WEST, jcbDegreeOfInflammatory);
//        mainLayout.putConstraint(SpringLayout.NORTH, jcbProcessByLocalization, 20, SpringLayout.SOUTH, jcbDegreeOfInflammatory);
//
//        mainLayout.putConstraint(SpringLayout.WEST, labelDiseaseDuration, 20, SpringLayout.WEST, panel);
//        mainLayout.putConstraint(SpringLayout.NORTH, labelDiseaseDuration, 20, SpringLayout.SOUTH, jcbProcessByLocalization);
//        mainLayout.putConstraint(SpringLayout.WEST, panelDiseaseDuration, 0, SpringLayout.WEST, jcbProcessByLocalization);
//        mainLayout.putConstraint(SpringLayout.NORTH, panelDiseaseDuration, 20, SpringLayout.SOUTH, jcbProcessByLocalization);

//        mainLayout.putConstraint(SpringLayout.WEST, labelAlcoholic, 20, SpringLayout.WEST, panel);
//        mainLayout.putConstraint(SpringLayout.NORTH, labelAlcoholic, 20, SpringLayout.SOUTH, panelDiseaseDuration);
//        mainLayout.putConstraint(SpringLayout.WEST, panelAlcoholic,0, SpringLayout.WEST, panelDiseaseDuration);
//        mainLayout.putConstraint(SpringLayout.NORTH, panelAlcoholic, 20, SpringLayout.SOUTH, panelDiseaseDuration);

//        mainLayout.putConstraint(SpringLayout.WEST, labelNarcomaniac, 20, SpringLayout.WEST, panel);
//        mainLayout.putConstraint(SpringLayout.NORTH, labelNarcomaniac, 20, SpringLayout.SOUTH, panelAlcoholic);
//        mainLayout.putConstraint(SpringLayout.WEST, panelNarcomaniac,0, SpringLayout.WEST, panelAlcoholic);
//        mainLayout.putConstraint(SpringLayout.NORTH, panelNarcomaniac, 20, SpringLayout.SOUTH, panelAlcoholic);
//
//        mainLayout.putConstraint(SpringLayout.WEST, labelNextButton, 20, SpringLayout.WEST, panel);
//        mainLayout.putConstraint(SpringLayout.NORTH, labelNextButton, 20, SpringLayout.SOUTH, panelNarcomaniac);
//        mainLayout.putConstraint(SpringLayout.WEST, nextButton,0, SpringLayout.WEST, panelNarcomaniac);
//        mainLayout.putConstraint(SpringLayout.NORTH, nextButton, 20, SpringLayout.SOUTH, panelNarcomaniac);

        mainLayout.putConstraint(SpringLayout.WEST, labelNextButton, 20, SpringLayout.WEST, panel);
        mainLayout.putConstraint(SpringLayout.NORTH, labelNextButton, 20, SpringLayout.SOUTH, jScrollPane);
        mainLayout.putConstraint(SpringLayout.WEST, nextButton, 0, SpringLayout.WEST, jScrollPane);
        mainLayout.putConstraint(SpringLayout.NORTH, nextButton, 20, SpringLayout.SOUTH, jScrollPane);
    }

    private List<Group> convertArrayToListGroups() {
        List<Group> groupList = new ArrayList<Group>();
        for (Object obj : jListGroup.getSelectedValuesList()) {
            Group group = (Group) obj;
            groupList.add(group);
        }
        return groupList;
    }
}
